package main

import (
	"fmt"

	"github.com/gin-gonic/gin"
)

// 中间件
func main() {

	r := gin.Default()
	// 1、作用于全局
	//r.Use(LoginAuth)
	r.POST("login", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "登录成功",
		})
	})
	// 2、作用于接口组
	user := r.Group("user", LoginAuth)
	{
		user.GET(":id", func(c *gin.Context) {
			id := c.Param("id")
			c.JSON(200, gin.H{
				"code": 200,
				"msg":  "查询id：" + id,
			})
		})
		user.PUT(":id", func(c *gin.Context) {
			id := c.Param("id")
			c.JSON(200, gin.H{
				"code": 200,
				"msg":  "更新id：" + id,
			})
		})
	}
	// 3、作用于单个接口
	r.DELETE("user/:id", beforDeleteHandle, deleteHandle)
	r.Run()
}

func deleteHandle(c *gin.Context) {
	id := c.Param("id")
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除id：" + id,
	})
}

func beforDeleteHandle(c *gin.Context) {
	fmt.Println("删除id前，进行登录接口保护")
}

func LoginAuth(c *gin.Context) {
	fmt.Println("我是登录保护中间件，login")
}
